class GameRebornPanel extends BasePanel {
	/** 有些结束页面*/
	public mainGroup:eui.Group;
	// 点击跳过按钮
	public skipBtn: eui.Image;
	private share2GroupBtn:eui.Image;
	private watchVideoBtn:eui.Image;

	private maxTime: number;
	public constructor(index: number = GameConfig.uiIndex.gameReborn) {
		super(index);
		this.maxTime = 10;
		this.skinName = "GameRebornPanelSkin";
	}
	public onChildrenCreated() {
		this.initView();
	}
	private initView() {
		// 没有出现使用复活币的弹窗页面展示10秒后自动消失
		this.startTime();
		// 获取复活币的用户信息
		this.requestUserInfo();
	}
	/**倒计时开始 */
	private startTime() {
		this.stopTime();
		egret.Tween.get(this).wait(1000).call(this.updateTime);
	}
	/**结束倒计时 */
	private stopTime() {
		egret.Tween.removeTweens(this);
		this.maxTime = 10;
	}
	/**刷新内容，如果倒计时为0时关闭当前页面 */
	private updateTime() {
		this.maxTime--;
		if (this.maxTime > 0) {
			this.startTime();
		} else {
			this.close();
		}
	}
	
	/**获取用户信息 */
	private times: number = 0;
	private requestUserInfo(time: number = 0) {
		if (this.times % 60 == 0) {
			App.HttpRequestManager.request(NetConstants.GET_INFO, "", this.responseUserInfo.bind(this), egret.HttpMethod.GET);
		}
		this.times++;
		return false;
		
	}
	private responseUserInfo(res) {
		if (res.statusCode == 200 && res.data.code == 0) {
			var myData = res.data.data;
			UserData.saveUserInfo(res.data.data);
			if (myData.reborn_coin > UserData.reborn_coin) {
				UserData.saveUserInfo(myData);
				if (UserData.relive_type == UserData.RebornType.SHARE) {
					TipsUtils.showTipsDownToUp("已有好友点击互助");
				}
			}
		} else {

		}
	}
    /**
     * 点击跳过
     */
	private close() {
		this.stopTime();
		App.EventManager.dispatchEvent(GameConstants.GAME_OVER);
		// 当前页面不能在此时删除，需要在gameover页面删除，这样才不会有突然闪一下的感觉
	}
    
	private share2Group(){
		this.stopTime();
		// 策略A
		let query = {from:GameConfig.shareFrom.homePage2Share};
		let shareData = GameConfig.getShareData(GameConfig.shareInfoA,query);
		Utils.shareAppMessageWithCheckGroup(shareData,(shareSuccess:boolean,shareTicket:string,iv:string,encryptedData:string)=>{
				if(UserData.is_verify){
					this.reLive();
					return;
				}
				if(!shareSuccess){
					 egret.Tween.get(this).wait(50).call(() => {
						TipsUtils.showTipsDownToUp("分享失败");
					 });
					 this.startTime();
					
				}
				else if(shareSuccess && !shareTicket){
					egret.Tween.get(this).wait(50).call(() => {
						TipsUtils.showTipsDownToUp("分享到群才能生效");
					 });
					this.startTime();
				}
				else if(shareTicket && !iv){
					egret.Tween.get(this).wait(50).call(() => {
						TipsUtils.showTipsDownToUp("获取分享信息失败~~~~");
					 });
					this.startTime();
				}
				else if(iv && encryptedData){
					this.startTime();
					this.requestShareResult(iv,encryptedData);
				}
				
			},this);
	}
	private requestShareResult(iv:string,encryptedData:string){
		let param = {iv:iv,encryptedData:encryptedData}
		App.HttpRequestManager.request(NetConstants.SHARE_DIFF_GROUP, param, this.responseShareResult.bind(this));
	}
	private responseShareResult(res){
		if(res.statusCode == 200 && res.data.code == 0){
			this.reLive();
		}else{
			this.startTime();
			 egret.Tween.get(this).wait(50).call(() => {
    			TipsUtils.showTipsDownToUp("每个群每天只能分享1次");
			 });
		}
	}
	/**
	 * 复活
	 */
	private reLive(){
		this.stopTime();
		egret.Tween.get(this).wait(50).call(() => {
			TipsUtils.showTipsDownToUp("复活成功");
		});
		this.stopTime();
		//复活成功
		App.EventManager.dispatchEvent(GameConstants.GAME_RELIVE);
		App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, this.index);
	}
	private watchVideo(evt:egret.TouchEvent){
		if(evt.type != egret.TouchEvent.TOUCH_END){
			return;
		}
		this.stopTime();
		if(StringUtil.sdkVersionCompare(GameConfig.SDKVersion,"2.0.4")){
			if(!GameConfig.videoAd){
				GameConfig.videoAd = wx.createRewardedVideoAd({
					 adUnitId: 'adunit-810fde3006393c9d'
				})
				GameConfig.videoAd.onClose((res)=>{
					if(res.isEnded){
						//复活成功
						App.EventManager.dispatchEvent(GameConstants.GAME_RELIVE);
						App.EventManager.dispatchEvent(GameConstants.UI_REMOVE_FROM_PARENT, this.index);
					}else{
						this.startTime();
						console.log("没有播放结束---");
					}
				});
				GameConfig.videoAd.onError((err)=>{
					console.log(">>watchVideo>onError",err);
					this.startTime();
					TipsUtils.showTipsDownToUp("没有视频广告了");
				});
			}
			GameConfig.videoAd.load().then(() => {
				GameConfig.videoAd.show();
			});
			
		}
	}

	/**添加监听 */
	public addListener() {
		super.addListener();
		this.skipBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.close, this);
		ViewUtils.addTapEvent(this.share2GroupBtn,this.share2Group,this,{scaleX:0.9,scaleY:0.9});
		ViewUtils.addTapEvent(this.watchVideoBtn,this.watchVideo,this,{scaleX:0.9,scaleY:0.9});
		
	}
	/**移除监听 */
	public removeListener() {
		super.removeListener();
		egret.Tween.removeTweens(this);
		this.skipBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.close, this);
		ViewUtils.removeTapEvent(this.share2GroupBtn,this);
		ViewUtils.removeTapEvent(this.watchVideoBtn,this);
	}

}